const fs = require('fs');

function underlineToHump(str) {
  if (str.match(/_(.)/g)) {
    for (let match of str.match(/_(.)/g)) {
      str = str.replace(match, match.replace('_', '').toUpperCase());
    }
  }
  return str;
}

function upFirst(str) {
  return str[0].toUpperCase() + str.substring(1);
}

function createFolder(path) {
  try {
    fs.mkdirSync(path);
  } catch (error) {}
}
/**
 * 把字符串变成等长字符串,前后加空格
 * @param {*} str 原始字符串
 * @param {*} length 最终字符串长度
 */
function equalLengthStr(str, length) {
  if (str.length >= length) {
    return str;
  } else {
    var startLength = parseInt((length - str.length) / 2);
    var endLength = length - str.length - startLength;
    var startStr = '';
    var endStr = '';
    for (let i = 0; i < startLength; i++) {
      startStr += ' ';
    }
    for (let i = 0; i < endLength; i++) {
      endStr += ' ';
    }
    return startStr + str + endStr;
  }
}

module.exports = {
  underlineToHump: underlineToHump,
  upFirst: upFirst,
  createFolder: createFolder,
  equalLengthStr: equalLengthStr
};
